//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OrthogonalProjections.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (Cephei::QL::Math::IMatrix^ originalVectors, Double multiplierCutOff, Double tolerance) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _CoriginalVectors;
    try
    {
#ifdef HANDLE
        _phOrthogonalProjections = NULL;
#endif
        _CoriginalVectors = safe_cast<CMatrix^> (originalVectors);
        _CoriginalVectors->Lock();
        QuantLib::Matrix& _originalVectors = static_cast<QuantLib::Matrix&> (_CoriginalVectors->GetReference ()); 
        QuantLib::Real _multiplierCutOff = (QuantLib::Real)ValueHelper::Convert (multiplierCutOff);
        QuantLib::Real _tolerance = (QuantLib::Real)ValueHelper::Convert (tolerance);
        _ppOrthogonalProjections = new boost::shared_ptr<QuantLib::OrthogonalProjections> (new QuantLib::OrthogonalProjections ( _originalVectors,  _multiplierCutOff,  _tolerance ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoriginalVectors != nullptr) _CoriginalVectors->Unlock();
    }
}
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (boost::shared_ptr<QuantLib::OrthogonalProjections>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalProjections = NULL;
#endif
	_ppOrthogonalProjections = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (QuantLib::OrthogonalProjections& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phOrthogonalProjections = NULL;
#endif
	_ppOrthogonalProjections = new boost::shared_ptr<QuantLib::OrthogonalProjections> (&childNative);
    
    _OrthogonalProjectionsOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (COrthogonalProjections^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalProjections = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOrthogonalProjections = new boost::shared_ptr<QuantLib::OrthogonalProjections> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalProjections = NULL;
#endif
	if (!t->IsSubclassOf(COrthogonalProjections::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (QuantLib::Handle<QuantLib::OrthogonalProjections>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOrthogonalProjections = &childNative;
	_ppOrthogonalProjections = &static_cast<boost::shared_ptr<QuantLib::OrthogonalProjections>>(childNative.currentLink());
    
    _OrthogonalProjectionsOwner = owner;
}
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (QuantLib::Handle<QuantLib::OrthogonalProjections> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOrthogonalProjections = &childNative;
	_ppOrthogonalProjections = &static_cast<boost::shared_ptr<QuantLib::OrthogonalProjections>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::COrthogonalProjections (QuantLib::OrthogonalProjections childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalProjections = NULL;
#endif
	_ppOrthogonalProjections = new boost::shared_ptr<QuantLib::OrthogonalProjections> (new QuantLib::OrthogonalProjections (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::COrthogonalProjections::~COrthogonalProjections ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppOrthogonalProjections != NULL)
    {
	    delete _ppOrthogonalProjections;
        _ppOrthogonalProjections = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::COrthogonalProjections::!COrthogonalProjections ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppOrthogonalProjections != NULL)
    {
	    delete _ppOrthogonalProjections;
    }
}
QuantLib::OrthogonalProjections& Cephei::QL::Math::Matrixutilities::COrthogonalProjections::GetReference ()
{
    if (_ppOrthogonalProjections == NULL) throw gcnew NativeNullException ();
	return **_ppOrthogonalProjections;
}
boost::shared_ptr<QuantLib::OrthogonalProjections>& Cephei::QL::Math::Matrixutilities::COrthogonalProjections::GetShared ()
{
    if (_ppOrthogonalProjections == NULL) throw gcnew NativeNullException ();
	return *_ppOrthogonalProjections;
}
QuantLib::OrthogonalProjections* Cephei::QL::Math::Matrixutilities::COrthogonalProjections::GetPointer ()
{
    if (_ppOrthogonalProjections == NULL) throw gcnew NativeNullException ();
	return &**_ppOrthogonalProjections;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OrthogonalProjections>& Cephei::QL::Math::Matrixutilities::COrthogonalProjections::GetHandle ()
{
	if (_phOrthogonalProjections == NULL)
	{
		_phOrthogonalProjections = new Handle<QuantLib::OrthogonalProjections> (*_ppOrthogonalProjections);
	}
	return *_phOrthogonalProjections;
}
#endif
bool Cephei::QL::Math::Matrixutilities::COrthogonalProjections::HasNative () 
{
	return (_ppOrthogonalProjections != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Math::Matrixutilities::COrthogonalProjections::GetVector (UInt64 index)
{
    try
    {
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index);
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppOrthogonalProjections)->GetVector ( _index );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Matrixutilities::COrthogonalProjections::NumberValidVectors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppOrthogonalProjections)->numberValidVectors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Matrixutilities::IOrthogonalProjections^ Cephei::QL::Math::Matrixutilities::COrthogonalProjections_Factory::Create (Cephei::QL::Math::IMatrix^ originalVectors, Double multiplierCutOff, Double tolerance)
{
    return gcnew COrthogonalProjections ( originalVectors,  multiplierCutOff,  tolerance);
}
